package com.lingnan.club.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lingnan.club.pojo.User;
import com.lingnan.club.service.UserService;
import com.lingnan.club.utils.Result;
import com.lingnan.club.utils.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 刘宇浩
 * @since 2022-04-02
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

   /**
     * 查询我的信息
     * 测试通过
     */
   @PostMapping("/myData")
   public Result myData(@RequestParam Integer id){
        User user = userService.findMyData(id);
        if (!ObjectUtils.isEmpty(user)){
            return ResultGenerator.genSuccessResult(200,user,"查询个人信息成功！");
        }
        return ResultGenerator.genErrorResult(201,"查询个人信息失败！");
    }

    /**
     * 打卡
     * @param id
     * @return
     */
    @PostMapping("/pushCard")
    public Result pushCard(@RequestParam String id){
        Boolean boolen = userService.pushCard(id);
        if (boolen){
            return ResultGenerator.genSuccessResult(200,"打卡成功！");
        }
        return ResultGenerator.genErrorResult(201,"打卡失败！");
    }

    /**
     * 登录
     * @param user
     * @return
     * 测试通过
     */
    @PostMapping("/login")
    public Result login(@RequestBody User user){
        String username = user.getUsername();
        String password = user.getPassword();
        if (StrUtil.isBlank(username) || StrUtil.isBlank(password)) {
            return ResultGenerator.genErrorResult(201, "参数错误");
        }
        User one = userService.login(user);
        if (!ObjectUtils.isEmpty(one)){
            return ResultGenerator.genSuccessResult(200,one,"登录成功！");
        }
        return ResultGenerator.genErrorResult(201,"登录失败！");
    }

    /**
     * 注册
     * @param user
     * @return
     * 测试通过
     */
    @PostMapping("/register")
    public Result register(@RequestBody User user){
        String username = user.getUsername();
        String password = user.getPassword();
        if (StrUtil.isBlank(username) || StrUtil.isBlank(password)) {
            return ResultGenerator.genErrorResult(201, "参数错误");
        }
        boolean flag = userService.register(user);
        if (flag){
            return ResultGenerator.genSuccessResult(200,"注册成功！");
        }
        return ResultGenerator.genErrorResult(201,"注册失败！");
    }

    /**
     * 查询在线时间排行榜
     * @return
     * 测试通过
     */
    @PostMapping("/orderOnlineTime")
    public Result orderOnlineTime(){
        List<User> list = userService.orderOnlineTime();
        if (!ObjectUtils.isEmpty(list)){
            return ResultGenerator.genSuccessResult(200,list,"查询在线时间排行榜成功！");
        }
        return ResultGenerator.genErrorResult(201,"查询在线时间排行榜失败！");
    }

}
